package main.java.LeetCode.ClassicalAlgorithm.Worker.Impl.chain;

import main.java.LeetCode.ClassicalAlgorithm.Queue.Queue;

public class Source<T> {
    private ThreadNode next;
    private Queue<T> out;
    private int size;

    public void flow(){
        ThreadNode temp = next;
        while (temp!=null){
            temp.getThread().start();
            temp = next.getNext();
        }
    }

    public void over(){
        ThreadNode node = next;
        while (next!=null){
            Thread thread = node.getThread();
            if(!thread.isInterrupted())
                thread.interrupt();
            next = next.getNext();
        }
    }

    public <E> GlobalChain<T,E> link(GlobalChain<T,E> chain){
        chain.setIn(this.out);
        this.setNext(chain.getThreadNode());
        chain.getThreadNode().setSize(this.size);
        return chain;
    }

    public ThreadNode getNext() {
        return next;
    }

    public void setNext(ThreadNode next) {
        this.next = next;
    }

    public Queue<T> getOut() {
        return out;
    }

    public void setOut(Queue<T> out) {
        this.out = out;
        this.size = out.size();
    }
}
